/*
 *    Copyright 2021 Xie Yu
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 *
 */

package link.xieyu;


import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfBitmap;
import com.spire.pdf.widget.PdfPageCollection;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author xieyu
 */
public class SpireApplication {
    public static void main(String[] args) {
        System.out.println("请输入你的pdf文件夹的绝对路径");
        Scanner scanner = new Scanner(System.in);
        File defaultFile;
        while (true) {
            try {
                String next = scanner.next();
                defaultFile = new File(next);
                if (defaultFile.exists()) {
                    break;
                }
                System.err.println("该路径不存在!");
            } catch (Exception e) {
                e.printStackTrace();
                System.err.println("输入的路径不合法,请正确输入!");
            }
        }
        ArrayList<String> files = new ArrayList<>();
        getAllFilePath(defaultFile, files);
        int index = 1;
        for (String filePath : files) {
            System.out.println("正在处理第" + index + "个文件,路径为:" + filePath);
            PdfDocument pdfDocument =
                    new PdfDocument(filePath);
            PdfPageCollection pages = pdfDocument.getPages();

            pages.insert(0);
            PdfPageBase pageBase = pages.get(1);
            BufferedImage[] bufferedImages = pageBase.extractImages();
            for (BufferedImage image : bufferedImages) {
                Graphics graphics = image.getGraphics();
                graphics.setColor(Color.WHITE);
                graphics.fillRect(324, 669, 260, 158);
                graphics.fillRect(324, 2044, 257, 169);
                graphics.fillRect(327, 2242, 254, 172);
                graphics.fillRect(324, 2437, 260, 180);
                graphics.fillRect(324, 2628, 260, 175);
                graphics.fillRect(5, 0, 1035, 84);
                pageBase.replaceImage(0, new PdfBitmap(image));

            }
            String fileSnap = filePath + ".pdf";
            pdfDocument.saveToFile(fileSnap);
            index++;
            pdfDocument.close();
            PdfReader reader = null;
            PdfStamper stamp = null;
            try {
                reader = new PdfReader(fileSnap);
                ArrayList<Integer> integers = new ArrayList<>();
                for (int i = 2; i < reader.getNumberOfPages(); i++) {
                    integers.add(i);
                }
                reader.selectPages(integers);
                stamp = new PdfStamper(reader, new FileOutputStream(filePath));
                File file = new File(fileSnap);
                file.deleteOnExit();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (DocumentException e) {
                e.printStackTrace();
            } finally {
                if (reader != null) {
                    reader.close();
                }
                if (stamp != null) {
                    try {
                        stamp.close();
                    } catch (DocumentException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        }


    }

    /**
     * 把所有的文件的路径的名加入到list集合中
     * 思路:
     * 首先获取当前路径的所有的文件的File的list集合
     * 遍历集合
     * 如果是文件就放入传入的list集合中
     * 如果不是文件是文件夹
     * 就递归调用自己,把该当前找到的文件夹在再传入方法,继续判断当前文件夹
     *
     * @param f     代表要寻找的文件夹的对应的File类
     * @param files List表示要把文件路径加入的list集合
     */
    public static void getAllFilePath(File f, List<String> files) {
        // 获取当前文件夹下的下一级的所有文件File类的集合
        File[] list = f.listFiles();
        // 遍历拿到的集合
        if (list != null) {
            for (File file : list) {
                // 如果是文件就直接加入传入的files集合中
                if (file.isFile()) {
                    if (!file.getName().endsWith(".pdf")) {
                        continue;
                    }
                    files.add(file.getAbsolutePath());
                    // 如果是文件夹就递归调用自己,把当前文件夹传入继续判断
                }
                else {
                    getAllFilePath(file, files);
                }
            }
        }
    }
}
